#include "PageLibPreprocessor.h"
#include "include/tinyxml2.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <utility>

using std::ifstream;
using std::cerr;
using std::istringstream;
using std::pair;

/* using namespace tinyxml2; */

PageLibPreprocessor::PageLibPreprocessor()
{
    //预留空间
    /* _pageList.reserve(4000); */    
}

PageLibPreprocessor:: ~PageLibPreprocessor()
{
    
}
    
void PageLibPreprocessor::readInfoFromFile()
{
    /* cout << "111" << endl; */
    //1、读网页库内容到vector<RSSItem>文件
    string filename = "data/ripepage.dat";
    string offFile = "data/offset.dat";
    ifstream ifsFile(filename);
    ifstream ifsOffset(offFile);
    if (!ifsFile) {
        cerr << "ifs open " << filename << "error!" << endl;
        return;
    }
    if (!ifsOffset) {
        cerr << "ifs open " << filename << "error!" << endl;
        return;
    }

        int cnt = 2;
    string lineOffset, fileContent;
    while (cnt-- && getline(ifsOffset, lineOffset)){//--测试只跑两遍
        
        //1、读网页库中的一篇文章
        istringstream iss(lineOffset);
        int id, begin, length;
        while (iss >> id) {
            iss >> begin >> length;
            /* cout << id << endl; */
            /* pair<int, int> offPair(begin, length); */
            /* cout << "pair " << offPair.first << "  " << offPair.second << endl; */

            
            string str(length, '\0');
            ifsFile.read(&str[0], length) ;
            /* cout << str << endl; */

            //交给WebPage构造函数处理
           WebPage tmp(str, _jieba);

            //测试
         /* cout << "PageLibPreprocessor" << endl; */
            int DocId = tmp.getDocId();
            cout << "docId " << DocId << endl;

           for (WebPage& elem : _pageList) {
               elem = tmp; //走的WebPage的拷贝构造函数
               //未设计堆空间，不用深拷贝。
           }
        
        } //2、把文件指针偏移到下一篇文章开始
        ifsFile.seekg(length);
            /* break; */
    }
    /* string docAll; */
    /* ifs >> docAll; */ 


    //2、关闭流
    ifsFile.close();
    ifsOffset.close();

    //填充WebPage类
   //一份停词库 
   
}

void PageLibPreprocessor::cutRedundantPage()
{

}

void PageLibPreprocessor::bulidInvertIndexMap()
{

}

void PageLibPreprocessor::storeOnDisk()
{

}
